package com.hooware.dao;

import com.hooware.domain.Permission;
import com.hooware.domain.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IRoleDao {

    @Select("select * from role")
    List<Role> findAll();

    @Insert("insert into role values(default, #{roleName}, #{roleDesc})")
    void saveRole(Role role);

    @Select("select * from role where id in (select roleid from users_role where userid = #{id}) ")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(many = @Many(select = "com.hooware.dao.IPermissionDao.findPermissionByRoleId"), column = "id", property = "permissions")
    })
    List<Role> findRoleByUserId(String id);

    @Select("select * from role where id = #{roleId}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(many = @Many(select = "com.hooware.dao.IPermissionDao.findPermissionByRoleId"),column = "id", property = "permissions")
    })
    Role findById(String roleId);

    @Select("select * from permission where id not in (select permissionId from role_permission where roleId = #{roleId})")
    List<Permission> findOtherPermission(String roleId);

    @Insert("insert into role_permission values(#{pid}, #{roleId})")
    void addPermissionToRole(@Param("roleId") String roleId, @Param("pid") String pid);
}
